package com.cupk.carManageMaster.core.serviceImpl;

import com.cupk.carManageMaster.common.domain.ResponseBase;
import com.cupk.carManageMaster.common.domain.User;
import com.cupk.carManageMaster.common.mapper.UserMapper;
import com.cupk.carManageMaster.core.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;
import java.util.UUID;

@Service
@Transactional
public class UserServiceImpl extends BaseApiServiceImpl implements UserService {

    @Override
    public ResponseBase userLogin(String jobNumber, String passWord) {
        //1.校验登陆是否成功
        User user = userMapper.getUserInfo(jobNumber, passWord);
        //2.如果不成功返回提示
        if (user == null) {
            return setResultError("账户名密码错误！！！");
        } else {
            //3.如果成功，生产一个token
            String token = UUID.randomUUID().toString().replaceAll("-", "");
            Map<String, String> mapInfo = productToken.productToken(user.getId().toString(), token);

//            mapInfo.put("token",token);
            mapInfo.put("userID", user.getId().toString());
            mapInfo.put("Name", user.getEmployee_name());
            mapInfo.put("url", user.getPhoto_url());
            mapInfo.put("Type", String.valueOf(user.getPrivilege_level()));
            //3.返回token信息（有效期100分钟）
            return setResultSuccess(mapInfo);
        }
    }

    @Override
    public ResponseBase getUserInfo(Integer userId) {
        User user = userMapper.getUser(userId);
        if (user != null) {
            return setResultSuccess(user);
        } else {
            return setResultSuccess("无此用户");
        }
    }

    @Autowired
    private ProductTokenServiceImpl productToken;
    @Autowired
    private UserMapper userMapper;


}
